package com.whr.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.whr.bean.*;
import com.whr.dao.ProblemMapper;
import com.whr.dao.RoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Transactional
@Service("probService")
public class ProbServiceImpl implements ProbService {
    @Resource
    private ProblemMapper problemMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<Problem> findAll() {
        return problemMapper.selectAll();
    }

    @Override
    public void saveProb(Problem prob) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = dateFormat.format(new Date());
        prob.setPdate(date);
        prob.setPstate("进行中");
        prob.setPfine("未加精");
        prob.setPrstate("未回复");
        prob.setProle("系统管理");
        prob.setPurg("");
        problemMapper.insert(prob);
    }

    @Override
    public Problem findById(Integer checks) {
        return problemMapper.selectByPrimaryKey(checks);
    }

    @Override
    public void updateProb(Problem prob) {
        problemMapper.updateByPrimaryKey(prob);
    }

    @Override
    public void addAppend(Integer pid, String text) {
        problemMapper.addAppend(pid, text);
    }

    @Override
    public List<Append> appendByPid(Integer pid) {
        return problemMapper.appendByPid(pid);
    }

    @Override
    public void delProb(Integer[] checks) {
        problemMapper.delProb(checks);
    }

    @Override
    public void urgProb(Integer[] checks) {
        problemMapper.urgProb(checks);
    }

    @Override
    public void fineProb(Integer[] checks) {
        problemMapper.fineProb(checks);
    }

    @Override
    public List<Problem> findAllOrder() {
        return problemMapper.selectAllOrder();
    }

    @Override
    public void endProb(Integer[] checks) {
        problemMapper.endProb(checks);
    }

    @Override
    public void addReply(Integer pid, String text) {
        problemMapper.addReplyState(pid);
        problemMapper.addReply(pid, text);
    }

    @Override
    public List<Reply> replyByPid(Integer pid) {
        return problemMapper.replyByPid(pid);
    }

    @Override
    public void addRaty(Integer pid, Double raty) {
        problemMapper.addRaty(pid, raty);
    }

    @Override
    public Double ratyByPid(Integer pid) {
        return problemMapper.ratyByPid(pid);
    }

    @Override
    public void updateRole(String role, Integer pid) {
        problemMapper.updateRole(role, pid);
    }

    @Override
    public List<Problem> findOver() {
        return problemMapper.findOver();
    }

    @Override
    public void upOver(Integer pid, String pover) {
        problemMapper.upOver(pid, pover);
    }

    @Override
    public void pass(Integer[] checks) {
        problemMapper.pass(checks);
    }

    @Override
    public void unpass(Integer[] checks) {
        problemMapper.unpass(checks);
    }

    @Override
    public List<Problem> findRep() {
        return problemMapper.findRep();
    }

    @Override
    public List<Problem> searchRep(Report report) {
        return problemMapper.searchRep(report);

    }

    @Override
    public List<Chart> chart() {
        List<Chart> charts = new ArrayList<>();
        List<ChartValue> cval = problemMapper.chartValue();
        List<String> roleNames = roleMapper.selRoleName();
        for (String rolename : roleNames) {
            Chart chart = new Chart();
            boolean isrole = false;
            for (ChartValue cv : cval){
                if (cv.getRol().equals(rolename)){
                    if (cv.getState().equals("进行中")) {
                        chart.setRolName(cv.getRol());
                        chart.setPing(cv.getNum());
                        isrole = true;
                    }
                    if (cv.getState().equals("已结束") ) {
                        chart.setRolName(cv.getRol());
                        chart.setPend(cv.getNum());
                        isrole = true;
                    }
                }
            }
            if (isrole) {
                charts.add(chart);
            }
        }
        for (String rolename : roleNames) {
            boolean flag = true;
            for (Chart chart : charts){
                if (chart.getRolName().equals(rolename)){
                    flag = false;
                }
            }
            if (flag){
                Chart chart0 = new Chart();
                chart0.setRolName(rolename);
                charts.add(chart0);
            }
        }
        return charts;
    }

    @Override
    public List<String> findProb7() {
        return problemMapper.findProb7();
    }

    @Override
    public List<String> findFine7() {
        return problemMapper.findFine7();
    }

    @Override
    public List<String> findReply5() {
        return problemMapper.findReply5();
    }

    @Override
    public JSONArray genJsonArry() {
        List<Problem> problems = problemMapper.selectExcel();
        JSONArray ja = JSONArray.parseArray(JSON.toJSONString(problems));
        return ja;
    }

    @Override
    public Map<String, String> genHeadMap() {
        Map<String, String> headMap = new LinkedHashMap<String, String>();
        headMap.put("ptitle", "标题");
        headMap.put("pdate", "创建时间");
        headMap.put("pstate", "状态");
        headMap.put("pfine", "是否精选");
        headMap.put("prole", "所属角色");
        headMap.put("prstate", "是否回复");
        return headMap;
    }

    @Override
    public List<Problem> findZN(String role) {
        return problemMapper.findZN(role);
    }

    @Override
    public List<Problem> findYX(String flag) {
        return problemMapper.findYX(flag);
    }

    @Override
    public List<Problem> findAllZN(String flag) {
        return problemMapper.findAllZN(flag);
    }

    @Override
    public List<Problem> findAllYX(String flag) {
        return problemMapper.findAllYX(flag);
    }
}